/*
 * @author zzr
 * @date: 2025/10/16  11:44
 * @description: 相同的树。给两颗二叉树的根节点 p q 判断这两棵树是否相同
 */

public class Demo20 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public boolean isSameTree(TreeNode p, TreeNode q) {

      // 一棵树不为空，另一棵树为空，则一定不相同
      if ((p != null && q == null) || (p == null && q != null)) {
        return false;
      }

      // 两棵树都为 null 则相同
      if (p == null && q == null) {
        return true;
      }

      // 一定要判断的是 不相等，返回 false
      if (p.val != q.val) {
        return false;
      }

      return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
    }
}
